package org.ezplatform.office.meeting.memo.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.ezplatform.core.dao.jpa.JpaBaseDao;
import org.ezplatform.core.service.BaseService;
import org.ezplatform.office.meeting.memo.dao.MemoUserInfoDao;
import org.ezplatform.office.meeting.memo.entity.MemoUserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class MemoUserInfoService extends BaseService<MemoUserInfo, String>{
    
    @Autowired
    private MemoUserInfoDao dao;

    @Override
    protected JpaBaseDao<MemoUserInfo, String> getEntityDao() {
        return dao;
    }

    /**
     * 查询会议纪要用户查看记录信息
     * @param id
     * @return
     */
    public List<MemoUserInfo> getMemoUserInfoList(String id) {
        return this.dao.getMemoUserInfoList(id);
    }

    /**
     * 删除会议纪要用户查看记录信息
     * @param memoId
     * @param existList
     */
    public void deleteMemoUserInfo(String memoId, List<String> existList) {
        if(existList!=null && existList.size()>0){
            this.dao.deleteMemoUserInfo(memoId,existList);
        }else{
            this.dao.deleteMemoUserInfo(memoId);
        }
    }

    /**
     * 新增会议纪要用户查看记录信息
     * @param memoId
     * @param addList
     * @param corpId 
     */
    public void addMemoUserInfo(String memoId, List<String> addList, String corpId) {
        if(CollectionUtils.isNotEmpty(addList)){
        	//List<String> infos = this.dao.getMemoUserInfo(memoId, addList, corpId);
        	
        	List<String> infos = new ArrayList();
        	List<MemoUserInfo> memoInfos = this.dao.getMemoUserInfoByMemoId(memoId, corpId);
        	for(MemoUserInfo inf : memoInfos){
        		if(addList.contains(inf.getMemoUserId())) {
        			infos.add(inf.getMemoUserId());
        		}
        	}
        	
            List<MemoUserInfo> list = new ArrayList<MemoUserInfo>();
            MemoUserInfo m = null;
            for(String userId : addList){
            	if(CollectionUtils.isEmpty(infos) || !infos.contains(userId)){
            		m = new MemoUserInfo();
                    m.setMemoId(memoId);
                    m.setMemoUserId(userId);
                    m.setIsView(0);
                    m.setCorpId(corpId);
                    list.add(m);
            	}
            }
            this.save(list);
        }
    }

    /**
     * 更新
     * @param id
     * @param userId
     */
    public void editMemoUserInfo(String memoId, String userId) {
        this.dao.updateMemoUserInfo(memoId,userId);
    }
    
    /**
     * 查询用户会议纪要信息
     * @param memoId
     * @param userId
     * @param corpId
     * @return
     */
    public MemoUserInfo getMemoUserInfo(String memoId,String userId,String corpId){
        List<MemoUserInfo> list = this.dao.getMemoUserInfo(memoId,userId,corpId);
        if(list!=null && list.size()>0){
            return list.get(0);
        }
        return null;
    }
    
    public List<MemoUserInfo> getMemoUserInfoByMemoId(String memoId, String corpId){
    	return dao.getMemoUserInfoByMemoId(memoId, corpId);
    }
}